
package com.xueyu.controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.xueyu.domain.role;
import com.xueyu.domain.rp;
import com.xueyu.util.ResultBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.File;
import java.text.SimpleDateFormat;
import com.xueyu.dao.roleDao;
import com.xueyu.dao.rpDao;
import java.util.*;
@CrossOrigin
@Controller
@RequestMapping("/roleController")
public class RoleController {
    @Autowired
    private roleDao roleDao;
    @Autowired
    private rpDao rpDao;
    @ResponseBody
    @RequestMapping(value = "/uploadPic",method = RequestMethod.POST)
    public  Map<String, Object> uploadPic(HttpServletRequest request, @ModelAttribute role role, HttpServletResponse response) throws Exception{
        ModelAndView modelAndView = new ModelAndView();
        Map<String, Object> map = new HashMap<>();
        if(role.getId()==0) {
            if (roleDao.insert(role) == 1) {
                map.put("code", 200);
                map.put("msg", "新增成功");
            } else {
                map.put("code", 500);
                map.put("msg", "新增失败!!");
            }
        }else {
            if (roleDao.updateById(role)==1) {
                map.put("code", 200);
                map.put("msg", "修改成功");
            } else {
                map.put("code", 500);
                map.put("msg", "修改失败!!");
            }
        }
        modelAndView.addObject("msg",map);
        return map;
    }
    //状态

    //删除
    @RequestMapping("/del/{id}")
    @ResponseBody
    public Map<String, Object> del(@PathVariable("id") int id){
        int rows = roleDao.deleteById(id);
        Map<String, Object> map = new HashMap<>();
        if (rows == 1) {
            map.put("code", 200);
            map.put("msg", "删除成功");
        } else {
            map.put("code", 500);
            map.put("msg", "删除失败!!");
        }
        return map;
    }

    @RequestMapping("/update/{id}")
    @ResponseBody
    public Map<String, Object> batchById(@PathVariable("id") int id){
        QueryWrapper<role> queryWrapper = Wrappers.query();
        queryWrapper.eq("id", id);  // 添加条件：slid等于指定的值
        List<role> roleList = roleDao.selectList(queryWrapper);
        Map<String, Object> map = new HashMap<>();
        if(roleList != null && !roleList.isEmpty()) {
            map.put("code", 200);
            map.put("msg", "查询成功");
            map.put("data", roleList);
        } else {
            map.put("code", 404);
            map.put("msg", "未找到对应角色");
        }
        return map;
    }


    @RequestMapping("/fy")
    @ResponseBody
    public Map<String, Object> fy(@RequestParam(value = "id", required = false) String id,
                                  @RequestParam(value = "role", required = false) String role,
                                  @RequestParam(value = "work", required = false) String work,
                                  HttpServletRequest request,long size,
                                  long Current)
    {
        size = Long.parseLong(request.getParameter("size"));
        Current = Long.parseLong(request.getParameter("Current"));
        Page<role> page = new Page<>(Current, size);
        id = request.getParameter("id");
        role = request.getParameter("role");
        work = request.getParameter("work");
        QueryWrapper<role> queryWrapper = Wrappers.query();
        if (id != null && !"".equals(id)) {
            queryWrapper.eq("id", id);
        }
        if (role != null && !"".equals(role)) {
            queryWrapper.like("role", role);
        }
        if (work != null && !"".equals(work)) {
            queryWrapper.like("work",  work );
        }
        page = roleDao.selectPage(page, queryWrapper);

        List<role> list = page.getRecords();
        Map<String, Object> map = new HashMap<>();
        map.put("page",page);
        map.put("list",list);
        return map;
    }


    @RequestMapping("/login")
    @ResponseBody
    public Map<String, Object> login() {
        List<role> list = roleDao.selectList(null);
        Map<String, Object> map = new HashMap<>();
        map.put("list",list);
        return map;
    }

    @RequestMapping("/list2")
    @ResponseBody
    public ResultBean fy2(
            @RequestBody  Map map
    ){
        String token =  (String)map.get("token");
    QueryWrapper<rp> queryWrapper = new QueryWrapper<rp>();
    queryWrapper.select("SPID"); // 指定要选择的字段
    queryWrapper.eq("RoleID",token);
    List<rp> SPIDlist= rpDao.selectList(queryWrapper);
        List<Integer> spidList = new ArrayList<>();
        for (rp rpItem : SPIDlist) {
            spidList.add(rpItem.getSPID());
        }
        ResultBean resultBean = new ResultBean();
        resultBean.put("list",spidList);
        return resultBean;
    }


    @RequestMapping("/zd")
    @ResponseBody
    public Map<String, Object> zd(@RequestParam(value = "id", required = false) String id,
                  @RequestParam(value = "role", required = false) String role,
                  @RequestParam(value = "work", required = false) String work,
                  HttpServletRequest request)
    {
        id = request.getParameter("id");
        role = request.getParameter("role");
        work = request.getParameter("work");
        QueryWrapper<role> queryWrapper = Wrappers.query();
        if (id != null && !"".equals(id)) {
            queryWrapper.eq("id", id);
        }
        if (role != null && !"".equals(role)) {
            queryWrapper.like("role", "%" + role + "%");
        }
        if (work != null && !"".equals(work)) {
            queryWrapper.like("work", "%" + work + "%");
        }
        List<role> list = roleDao.selectList(queryWrapper);
        Map<String, Object> map = new HashMap<>();
        map.put("list",list);
        return map;
    }


}




